package cpu.footprint.adapter;

import android.graphics.Bitmap;
import android.net.Uri;
import android.widget.BaseAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import cpu.footprint.R;
import cpu.footprint.api.ApiUrl;
import cpu.footprint.bean.ChatListViewItem;
import cpu.footprint.util.FileUtil;
import cpu.footprint.util.ImageDownloader;
import cpu.footprint.util.OnImageDownload;
/**
 * Created by Administrator on 2017/3/22.
 */

public class ChatListViewAdapter extends BaseAdapter {
    private ArrayList<ChatListViewItem> mList;
    private LayoutInflater mInflater;
    private ImageDownloader mDownloader;
    private float  wid;
    private Context context;
    ListView listview_active;
    public ChatListViewAdapter(Context tContext, ArrayList<ChatListViewItem> tList, ListView listView) {
        mInflater = LayoutInflater.from(tContext);
        mList = tList;
        context=tContext;
        listview_active=listView;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int tType = getItemViewType(position);
        ViewHolder tHolder = null;
        if (convertView == null) {
            if (tType == ChatListViewItem.ITEM_TYPE.TYPE_CENTER.ordinal()) {
                convertView = mInflater.inflate(R.layout.chat_item_center, parent, false);
            } else if (tType == ChatListViewItem.ITEM_TYPE.TYPE_LEFT.ordinal()) {
                convertView = mInflater.inflate(R.layout.chat_item_left, parent, false);
            } else if (tType == ChatListViewItem.ITEM_TYPE.TYPE_RIGHT.ordinal()) {
                convertView = mInflater.inflate(R.layout.chat_item_right, parent, false);
            }
            tHolder = new ViewHolder();
            convertView.setTag(tHolder);
        } else {
            tHolder = (ViewHolder) convertView.getTag();
        }
        if (tType == ChatListViewItem.ITEM_TYPE.TYPE_CENTER.ordinal()) {
            tHolder.getViewById(convertView, R.id.item_center_content).setText(
                    mList.get(position).getContent());
        } else if (tType == ChatListViewItem.ITEM_TYPE.TYPE_LEFT.ordinal()) {
            tHolder.getViewById(convertView, R.id.text_fromName).setText(mList.get(position).getmFrom());
            TextView tv=tHolder.getViewById(convertView, R.id.item_left_content);
            tv.setText(mList.get(position).getContent());
            ImageView imageView=tHolder.getImageViewById(convertView,R.id.item_left_portrait);
            //String fileName = ApiUrl.URL_IMG_HOST + mList.get(position).getmFrom()+".png";
            Picasso.with(context).load(ApiUrl.URL_IMG_HOST +mList.get(position).getmFrom()+".png").placeholder(R.mipmap.head).into(imageView);
            //DownImage(imageView,fileName);
        } else if (tType == ChatListViewItem.ITEM_TYPE.TYPE_RIGHT.ordinal()) {
            TextView tv=tHolder.getViewById(convertView, R.id.item_right_content);
            tv.setText(mList.get(position).getContent());
            ImageView imageView=tHolder.getImageViewById(convertView,R.id.item_right_portrait);
            //String fileName =ApiUrl. URL_IMG_HOST + mList.get(position).getmFrom()+"_ph.jpg";
            Picasso.with(context).load(ApiUrl.URL_IMG_HOST +mList.get(position).getmFrom()+".png").placeholder(R.mipmap.head).into(imageView);
            //DownImage(imageView,fileName);
        }
        return convertView;
    }
    /**
     * 获得某个条目的类型
     */
    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getType().ordinal();
    }

    /**
     * 获得类型总数
     */
    @Override
    public int getViewTypeCount() {
        return ChatListViewItem.ITEM_TYPE.values().length;
    }

    private class ViewHolder {
        HashMap<Integer, TextView> mMap = new HashMap<Integer, TextView>();
        public TextView getViewById(View tParent, int tID) {
            TextView tView = mMap.get(Integer.valueOf(tID));
            if (tView == null) {
                tView = (TextView) tParent.findViewById(tID);
                mMap.put(Integer.valueOf(tID), tView);
            }
            return tView;
        }
        HashMap<Integer, ImageView> mimageMap = new HashMap<Integer, ImageView>();
        public ImageView getImageViewById(View tParent, int tID) {
            ImageView tView = mimageMap.get(Integer.valueOf(tID));
            if (tView == null) {
                tView = (ImageView) tParent.findViewById(tID);
                mimageMap.put(Integer.valueOf(tID), tView);
            }
            return tView;
        }
    }
    private void DownImage(ImageView imageView, String path) {
        String url = path;
        imageView.setTag(url);
        if (mDownloader == null) {
            mDownloader = new ImageDownloader();
        }
        if (mDownloader != null) {
            //异步下载图片
            mDownloader.imageDownload(url, imageView, "/footprint", context, new OnImageDownload() {
                @Override
                public void onDownloadSucc(Bitmap bitmap,
                                           String c_url, ImageView mimageView) {
                    ImageView imageView = (ImageView) listview_active.findViewWithTag(c_url);
                    if (imageView != null) {
                        imageView.setImageBitmap(bitmap);
                        imageView.setTag("");
                    }
                }
            });
        }
    }
}
